<?php


namespace app\mall\model\Order;


use think\facade\Db;
use think\Model;
use think\model\relation\HasMany;

class OrderModel extends Model
{
    /**
     * 数据表名
     * @var string
     */
    protected $name = "order";

    /**
     * 关联定义
     * @return HasMany
     */
    public function list()
    {
        return $this->hasMany(OrderGoodsModel::class,'order_id','id');
    }

    /**
     * 根据条件查询单条数据
     * @param $where
     * @param string $field
     * @return mixed
     * @throws
     */
    public static function getOrderWhere($where,$field="*")
    {
        $goods = OrderModel::with('list')->field($field)->where($where)->find();
        return $goods;
    }

    /**
     * 根据条件查询多条数据
     * @param $where
     * @return mixed
     * @throws
     */
    public static function getOrderWhereMore($where,$field="*")
    {
        $list = OrderModel::with('list')
            ->field($field)
            ->where($where)
            ->select()->toArray();
        return $list;
    }

    /**
     * @param $page
     * @param $limit
     * @param array $where
     * @param string $field
     * @return mixed
     */
    public static function getOrderListWhereJoin($page,$limit,$where = [],$field="o.*")
    {
        $start = ceil(($page-1)*$limit);
        $list = self::alias('o')
            ->join('collage c','c.id = o.collage_id')
            ->with('list')
            ->field($field)
            ->where($where)
            ->limit($start,$limit)
            ->select()->toArray();
        return $list;
    }

    /**
     * 统计总数
     * @param $where
     * @return mixed
     */
    public static function getOrderCountWhereJoin($where)
    {
        $count = self::alias('o')
            ->join('collage c','c.id = o.collage_id')
            ->where($where)->count();
        return $count;
    }

    /**
     * 统计总数
     * @param $where
     * @return int
     */
    public static function getOrderCountWhere($where)
    {
        $count = OrderModel::where($where)->count();
        return $count;
    }

    /**
     * 分页查询
     * @param $page
     * @param $limit
     * @param $where
     * @return mixed
     * @throws
     */
    public static function getOrderListWhere($page,$limit,$where = [],$field="*")
    {
        $start = ceil(($page-1)*$limit);
        $list = OrderModel::with('list')
            ->field($field)
            ->where($where)
            ->limit($start,$limit)
            ->select()->toArray();
        return $list;
    }

    /**
     * 添加返回ID
     * @param $data
     * @return int
     */
    public static function addOrder($data)
    {
        $id = OrderModel::insertGetId($data);
        return $id;
    }

    /**
     * 更新
     * @param $where
     * @param $save
     * @return int
     * @throws
     */
    public static function updateOrderWhere($where,$save)
    {
        $flag = Db::name('order')->where($where)->update($save);
        return $flag;
    }

    /**
     * 删除
     * @param $where
     * @return int
     * @throws
     */
    public static function delOrderWhere($where)
    {
        $flag = Db::name('order')->where($where)->delete();
        return $flag;
    }
}